import torch
import torch.nn as nn
from hornet_block import HornetLiteBlock

class HornetBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride=1):
        super(HornetBlock, self).__init__()
        self.hornet_lite = HornetLiteBlock(in_channels, out_channels, stride)

        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        out = self.hornet_lite(x)
        return self.relu(out)


class HornetOnlyResNet(nn.Module):
    def __init__(self, num_classes=100):
        super(HornetOnlyResNet, self).__init__()
        self.stem = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True)
        )
        self.layer1 = self._make_layer(64, 64, num_blocks=2)
        self.layer2 = self._make_layer(64, 128, num_blocks=2, stride=2)
        self.layer3 = self._make_layer(128, 256, num_blocks=2, stride=2)
        self.layer4 = self._make_layer(256, 512, num_blocks=2, stride=2)
        self.pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(512, num_classes)

    def _make_layer(self, in_channels, out_channels, num_blocks, stride=1):
        layers = [HornetBlock(in_channels, out_channels, stride)]
        for _ in range(1, num_blocks):
            layers.append(HornetBlock(out_channels, out_channels))
        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.stem(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.pool(x).flatten(1)
        return self.fc(x)
